package handler

import (
	"github.com/gin-gonic/gin"
	"house/api/requset"
	"house/api/resp"
	"house/consts"
	"house/internal/dao"
	"house/internal/model"
	"house/internal/queue"
	"house/pkg"
)

// 发布房源
func AddHouse(c *gin.Context) {
	//接收参数
	var req requset.AddHouseReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "验证失败", err.Error())
		return
	}

	//业务逻辑
	//权限
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.UType == 2 || user.UType == 3) {
		resp.Error(c, 10000, "权限不足,禁止发布", nil)
		return
	}
	//内容审核
	if !TextContent(req.Title, c.GetUint("userId"), "房源发布") {
		resp.Error(c, 10000, "出现敏感词,房源发布失败", nil)
		return
	}
	//发布
	result := dao.Create(&model.House{
		UserId:    c.GetUint("userId"),
		Title:     req.Title,
		Price:     req.Price,
		City:      req.City,
		Address:   req.Address,
		Community: req.Community,
		CoverUrl:  req.CoverUrl,
		VideoUrl:  req.VideoUrl,
		Status:    req.Status,
	})
	if !result {
		resp.Error(c, 10000, "发布房源失败", false)
		return
	}
	//返回
	resp.Success(c, true)

}

// 内容审核
func TextContent(content string, userId uint, bName string) bool {
	if pkg.TextCensorContent(content) {
		return true
	}
	dao.Create(&model.Safe{
		Content: content,
		UserId:  userId,
		BName:   bName,
	})

	return false
}

// 房源列表
func GetHouseList(c *gin.Context) {
	//接收参数
	var req requset.GetHouseListReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "验证失败", err.Error())
		return
	}

	//业务逻辑
	pageDto := dao.PageDto{
		Page:     req.Page,
		PageSize: req.PageSize,
		OrderBy:  "created_at desc",
	}
	var houses []model.House
	paginate, err := dao.Paginate(&model.House{
		Title: req.Title,
	}, pageDto, &houses)
	if err != nil {
		resp.Error(c, 10000, "房源列表展示失败", false)
		return
	}

	//返回
	resp.Success(c, paginate)
}

// 房源详情
func GetHouseDetail(c *gin.Context) {
	//接收参数
	var req requset.GetHouseDetailReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "验证失败", err.Error())
		return
	}

	//业务逻辑
	var house model.House
	dao.GetOneById(req.Id, &house)
	if house.Id == 0 {
		resp.Error(c, 10000, "房源不存在", false)
		return
	}

	//返回
	resp.Success(c, house)
}

// 房价调整
func UpdateHousePrice(c *gin.Context) {
	//接收参数
	var req requset.UpdateHousePriceReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "验证失败", err.Error())
		return
	}

	//业务逻辑
	//权限
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.UType == 2 || user.UType == 3) {
		resp.Error(c, 10000, "权限不足", nil)
		return
	}
	//判断房源
	var house model.House
	dao.GetOneById(req.Id, &house)
	if house.Id == 0 {
		resp.Error(c, 10000, "房源不存在", nil)
		return
	}
	//判断价格
	if req.Price > 0 && (req.Price != house.Price) {
		go queue.PublishPub(consts.UPDATE_HOUSE_PRICE, model.House{
			Id:    req.Id,
			Title: house.Title,
			Price: req.Price,
		})
	}
	dao.Update(&model.House{
		Id:        req.Id,
		UserId:    c.GetUint("userId"),
		Title:     req.Title,
		Price:     req.Price,
		City:      req.City,
		Address:   req.Address,
		Community: req.Community,
		CoverUrl:  req.CoverUrl,
		VideoUrl:  req.VideoUrl,
		Status:    1, //1为在售
	})

	//返回
	resp.Success(c, true)

}
